import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  HttpException,
  HttpStatus,
  UseFilters,
} from '@nestjs/common';
import { CoffeesService } from './coffees.service';
import createCoffeesSchema from './createCoffeesSchema';
import { CreateCoffeeDto } from './dto/create-coffee.dto';
import { UpdateCoffeeDto } from './dto/update-coffee.dto';
import { CoffeesExceptionFilter } from './filters/coffees.filter';
import { CusPipe } from './pipes/cus.pipe';

@UseFilters(CoffeesExceptionFilter)
@Controller('coffees')
export class CoffeesController {
  constructor(private readonly coffeesService: CoffeesService) {}

  @Post()
  create(
    @Body(new CusPipe(createCoffeesSchema)) createCoffeeDto: CreateCoffeeDto,
  ) {
    return this.coffeesService.create(createCoffeeDto);
  }

  @Get('/exc')
  coffeeException() {
    console.log('here');
    throw new HttpException({ msg: 'Forbidden' }, HttpStatus.FORBIDDEN);
  }

  @Get()
  findAll() {
    return this.coffeesService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.coffeesService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateCoffeeDto: UpdateCoffeeDto) {
    return this.coffeesService.update(+id, updateCoffeeDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.coffeesService.remove(+id);
  }
}
